﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class LotAttributeCodeDA : ILotAttributeCodeDA
    {
        private string CODE_ID = "@CODE_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string DESCR2 = "@DESCR2";
        private string GROUP_ID = "@GROUP_ID";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string SEQ_NO = "@SEQ_NO";
        private string SQL_CHECK_LOT_ATTRIBUTE_CODE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@LOT_ATTRIBUTE_CODE WHERE CODE_ID = @CODE_ID AND GROUP_ID = @GROUP_ID ";
        private string SQL_DELETE_LOT_ATTRIBUTE_CODE = " DELETE FROM @_@LOT_ATTRIBUTE_CODE WHERE CODE_ID = @CODE_ID AND GROUP_ID = @GROUP_ID ";
        private string SQL_DELETE_LOT_ATTRIBUTE_CODE_BY_GROUP_ID = " DELETE FROM @_@LOT_ATTRIBUTE_CODE WHERE GROUP_ID = @GROUP_ID ";
        private string SQL_INSERT_LOT_ATTRIBUTE_CODE = " INSERT INTO @_@LOT_ATTRIBUTE_CODE ( WH_ID, GROUP_ID, CODE_ID, DESCR, DESCR2, SEQ_NO, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @GROUP_ID, @CODE_ID, @DESCR, @DESCR2, @SEQ_NO, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE = " SELECT  ROWID, WH_ID, GROUP_ID, CODE_ID, SEQ_NO, DESCR, DESCR2, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@LOT_ATTRIBUTE_CODE ";
        private string SQL_UPDATE_LOT_ATTRIBUTE_CODE = " UPDATE @_@LOT_ATTRIBUTE_CODE SET WH_ID = @WH_ID, DESCR = @DESCR, DESCR2 = @DESCR2, SEQ_NO = @SEQ_NO, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE CODE_ID = @CODE_ID AND GROUP_ID = @GROUP_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckLotAttributeCodeIDUnique(string groupID, string codeID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CODE_ID", codeID), new SqlParameter("@GROUP_ID", groupID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_LOT_ATTRIBUTE_CODE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckLotAttributeCodeIDUnique(DataBase dataBase, DbTransaction tran, string groupID, string codeID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CODE_ID", codeID), new SqlParameter("@GROUP_ID", groupID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_LOT_ATTRIBUTE_CODE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteLotAttributeCode(List<string> groupID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < groupID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GROUP_ID", groupID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOT_ATTRIBUTE_CODE_BY_GROUP_ID.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteLotAttributeCode(List<string> groupID, List<string> codeID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (groupID.Count != codeID.Count)
                {
                    throw new Exception("groupID和codeID数量不对应!");
                }
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < groupID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GROUP_ID", groupID[j]), new SqlParameter("@CODE_ID", codeID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteLotAttributeCode(DataBase dataBase, DbTransaction tran, List<string> groupID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < groupID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GROUP_ID", groupID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOT_ATTRIBUTE_CODE_BY_GROUP_ID.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteLotAttributeCode(DataBase dataBase, DbTransaction tran, List<string> groupID, List<string> codeID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                if (groupID.Count != codeID.Count)
                {
                    throw new Exception("groupID和codeID数量不对应!");
                }
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < groupID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GROUP_ID", groupID[j]), new SqlParameter("@CODE_ID", codeID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                return result;
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllLotAttributeCode(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllLotAttributeCode(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public LotAttributeCodeInfo GetLotAttributeCodeByID(string groupID, string codeID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID) + " WHERE CODE_ID = @CODE_ID AND GROUP_ID = @GROUP_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CODE_ID", codeID), new SqlParameter("@GROUP_ID", groupID) };
            LotAttributeCodeInfo lotAttributeCodeInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    lotAttributeCodeInfo = new LotAttributeCodeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GROUP_ID"].ToString(), reader["CODE_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["DESCR"].ToString(), reader["DESCR2"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return lotAttributeCodeInfo;
        }

        public LotAttributeCodeInfo GetLotAttributeCodeByID(DataBase dataBase, DbTransaction tran, string groupID, string codeID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID) + " WHERE CODE_ID = @CODE_ID AND GROUP_ID = @GROUP_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CODE_ID", codeID), new SqlParameter("@GROUP_ID", groupID) };
            LotAttributeCodeInfo lotAttributeCodeInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                lotAttributeCodeInfo = new LotAttributeCodeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GROUP_ID"].ToString(), reader["CODE_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["DESCR"].ToString(), reader["DESCR2"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return lotAttributeCodeInfo;
        }

        public DataSet GetLotAttributeCodeByQueryList(List<string> sqlWhere, LotAttributeCodeQueryEntity lotattributecodeQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!lotattributecodeQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, lotattributecodeQuery.CurrentPage, lotattributecodeQuery.PageSize, lotattributecodeQuery.SortField, lotattributecodeQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetLotAttributeCodeByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, LotAttributeCodeQueryEntity lotattributecodeQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!lotattributecodeQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, lotattributecodeQuery.CurrentPage, lotattributecodeQuery.PageSize, lotattributecodeQuery.SortField, lotattributecodeQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<LotAttributeCodeInfo> GetLotAttributeCodeListByID(string codeID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID) + " WHERE  CODE_ID = @CODE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CODE_ID", codeID) };
            List<LotAttributeCodeInfo> lotAttributeCodeInfos = new List<LotAttributeCodeInfo>();
            LotAttributeCodeInfo lotAttributeCode = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    lotAttributeCode = new LotAttributeCodeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GROUP_ID"].ToString(), reader["CODE_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["DESCR"].ToString(), reader["DESCR2"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    lotAttributeCodeInfos.Add(lotAttributeCode);
                }
            }
            return lotAttributeCodeInfos;
        }

        public List<LotAttributeCodeInfo> GetLotAttributeCodeListByID(DataBase dataBase, DbTransaction tran, string codeID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID) + " WHERE  CODE_ID = @CODE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CODE_ID", codeID) };
            List<LotAttributeCodeInfo> lotAttributeCodeInfos = new List<LotAttributeCodeInfo>();
            LotAttributeCodeInfo lotAttributeCode = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                lotAttributeCode = new LotAttributeCodeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GROUP_ID"].ToString(), reader["CODE_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["DESCR"].ToString(), reader["DESCR2"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                lotAttributeCodeInfos.Add(lotAttributeCode);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return lotAttributeCodeInfos;
        }

        public int InsertLotAttributeCode(LotAttributeCodeInfo lotAttributeCodeInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    lotAttributeCodeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LotAttributeCode_Parameters(lotAttributeCodeInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertLotAttributeCode(DataBase dataBase, DbTransaction tran, LotAttributeCodeInfo lotAttributeCodeInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    lotAttributeCodeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LotAttributeCode_Parameters(lotAttributeCodeInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_LotAttributeCode_Parameters(LotAttributeCodeInfo lotAttributeCodeInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.GROUP_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CODE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.DESCR, SqlDbType.VarChar, 100), new SqlParameter(this.DESCR2, SqlDbType.VarChar, 100), new SqlParameter(this.SEQ_NO, SqlDbType.Int, 4), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(lotAttributeCodeInfo.WhID))
            {
                paramArray[0].Value = lotAttributeCodeInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = lotAttributeCodeInfo.GroupID;
            paramArray[2].Value = lotAttributeCodeInfo.CodeID;
            if (!string.IsNullOrEmpty(lotAttributeCodeInfo.Descr))
            {
                paramArray[3].Value = lotAttributeCodeInfo.Descr;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(lotAttributeCodeInfo.Descr2))
            {
                paramArray[4].Value = lotAttributeCodeInfo.Descr2;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            paramArray[5].Value = lotAttributeCodeInfo.SeqNo;
            paramArray[6].Value = lotAttributeCodeInfo.IsActive;
            paramArray[7].Value = lotAttributeCodeInfo.CreatedBy;
            paramArray[8].Value = lotAttributeCodeInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateLotAttributeCode(LotAttributeCodeInfo lotAttributeCodeInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    lotAttributeCodeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LotAttributeCode_Parameters(lotAttributeCodeInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateLotAttributeCode(DataBase dataBase, DbTransaction tran, LotAttributeCodeInfo lotAttributeCodeInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    lotAttributeCodeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LotAttributeCode_Parameters(lotAttributeCodeInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOT_ATTRIBUTE_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

